package cn.hl.hm.logic.manage;

import cn.hl.hm.constants.DriverVehicleStatus;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.Push;
import cn.hl.hm.logic.service.IDriverService;
import cn.hl.hm.service.DriverOwnInfoService;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.DriverVehicleService;
import cn.hl.hm.service.PushToAppIService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.vo.VehicleVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.sql.SQLDataException;
import java.util.List;

/**
 * @description: 车辆逻辑处理
 * @Author: xianghan
 * @Date: 2017/10/27 14:51
 **/
@Service
public class VehicleManageLogic {
    private static final Logger logger = LoggerFactory.getLogger(VehicleManageLogic.class);
    @Autowired
    IDriverService iDriverService;
    @Autowired
    DriverVehicleService driverVehicleService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private PushToAppIService pushToAppIService;
    @Autowired
    private DriverOwnInfoService driverOwnInfoService;

    public void getVehicleList(Integer driverId,String plateNumber,String checkState,ModelMap modelMap) throws
            Exception{
        modelMap.put("vehicleList",iDriverService.getVehicleList(driverVehicleService.selectByIdAndNumberAndState(driverId, plateNumber, checkState)));
        modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
    }

    public void addVehicle(Integer driverId,String plateNumber,Integer vehicleTpyeId,Integer vehicleLengthId,
                           String transportLicense,String drivingLicensePic,String drivingLicenseGuaPic,
                           ModelMap modelMap){
        try {
            if (iDriverService.addVehicle( driverId,  plateNumber,  vehicleTpyeId,  vehicleLengthId,
                     transportLicense,  drivingLicensePic,  drivingLicenseGuaPic,1)) {
                modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
            }else{
                modelMap.addAttribute("infoCode", InfoCode.FAILURE);
            }
        }
        catch (SQLDataException e) {
            modelMap.addAttribute("infoCode", InfoCode.VEHICLE_EXIXTS_ERROR);
        }
    }
    public void deleteVehicle(Integer driverId, Integer vehicleId,ModelMap modelMap){
        iDriverService.deleteVehicle(driverId, vehicleId);
        modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
    }
    public void updateVehicle(String manageToken,Integer driverId, String plateNumber, Integer vehicleTpyeId, Integer
            vehicleLengthId,String transportLicense, String drivingLicensePic, String drivingLicenseGuaPic,
                              Integer status,String assessorUsername,String driverUnpassReason, ModelMap
                                      modelMap) {
        //TODO 有待修改
        if (iDriverService.updateVehicle(driverId, plateNumber, vehicleTpyeId, vehicleLengthId,
                transportLicense, drivingLicensePic, drivingLicenseGuaPic,status,assessorUsername,
                driverUnpassReason)) {
            Driver driver = driverService.selectByPrimaryKey(driverId);
            if (driver != null && StringUtils.isNotBlank(driver.getDeviceToken())) {
                Push push = new Push();
                push.setBizzType(Push.BizzTypeEnum.USER.getName());
                push.setPushoid("");
                push.setDisplayType("notification");// message:// 消息，通知：notification
                push.setOperationType(1);// 用户类型：1司机，2发货端
                push.setPushClient("driverPushClient");// 使用的推送Client
                push.setPushType("listcast");// unicast：单播 // listcast：列表
                push.setTokenStr(new String[]{driver.getDeviceToken()}); // 要发送手机的token
                // ios & android推送公共调用方法
                if (status!=null) {
                    if (status == (DriverVehicleStatus.PASS.getStatus().intValue())) {
                        push.setContext("恭喜您的车辆" + plateNumber + "审核成功，马上可以接单啦。");// 要发送的内容
                        push.setTitle("车辆审核通过"); // 发送的标题
                        push.setPtype(Push.PushTypeEnum.USERPUSH6.getId());
                    }else if(status == (DriverVehicleStatus.FAIL.getStatus().intValue())){
                        push.setContext("温馨提示，您的车辆"+plateNumber+"审核未通过，请至个人重新查看并重新提交。");// 要发送的内容
                        push.setTitle("车辆审核失败"); // 发送的标题
                        push.setPtype(Push.PushTypeEnum.USERPUSH7.getId());
                    }
                }
                driverOwnInfoService.insertSelective(push.getContext(),driverId);
                pushToAppIService.pushCommon(push);
            }
            modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
        }else {
            modelMap.addAttribute("infoCode", InfoCode.FAILURE);
        }

    }

    public void vehicleDetail(Integer driverId, Integer vehicleId,ModelMap modelMap) {
        List<VehicleVo> vehicleList = iDriverService.getVehicleList(driverVehicleService.selectByDIVI
                (driverId,vehicleId));
        if (vehicleList!=null) {
            modelMap.addAttribute("vehicle", vehicleList.get(0));
        }
        modelMap.addAttribute("infoCode", InfoCode.SUCCESS);
    }
}
